package io.kiki.sba.registry.server.clientInterface.predicate;

import io.kiki.sba.registry.core.model.Scope;
import io.kiki.sba.registry.server.clientInterface.bootstrap.Config;

import java.util.function.Predicate;


public final class ZonePredicate {
    private ZonePredicate() {
    }

    public static Predicate<String> pushDataPredicate(String dataId, String clientCell, Scope scope, Config config) {
        Predicate<String> predicate = (zone) -> zoneFilter(dataId, clientCell, scope, config, zone);
        return predicate;
    }

    private static boolean zoneFilter(String dataId, String clientCell, Scope scope, Config config, String zone) {
        if (!clientCell.equals(zone)) {
            if (Scope.zone == scope) {
                // zone scope subscribe only return zone list
                return true;

            } else
                if (Scope.data_center == scope || Scope.global == scope) {
                    // disable zone config
                    if (config.isInvalidForeverZone(zone) && !config.isInvalidIgnored(dataId)) {
                        return true;
                    }
                }
        }
        return false;
    }
}
